package com.uzai.console.web.alarm;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.dto.alarm.alarmcontacter.AlarmContacterQuery;
import com.uzai.console.dto.alarm.alarmrule.AlarmRuleIdDto;
import com.uzai.console.dto.alarm.alarmrule.AlarmRuleInfoDto;
import com.uzai.console.dto.alarm.alarmrule.AlarmRuleQuery;
import com.uzai.console.dto.alarm.alarmrule.AlarmRuleSetStatusDto;
import com.uzai.console.dto.dingding.DingWarningGroupQuery;
import com.uzai.console.entity.AlarmContacter;
import com.uzai.console.entity.DingWarningGroup;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.AlarmContacterMapper;
import com.uzai.console.mapper.DingWarningGroupMapper;
import com.uzai.console.service.alarm.AlarmRuleService;
import com.uzai.console.vo.alarm.alarmrule.AlarmRuleInfoVo;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 报警规则
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "报警规则",tags = "云监控|设备监控|报警规则")
@ApiSort(19)
@RestController
public class AlarmRuleController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(AlarmRuleController.class);

    @Autowired
    private AlarmRuleService alarmRuleService;
    @Autowired
    private AlarmContacterMapper alarmContacterMapper;
    @Autowired
    private DingWarningGroupMapper dingWarningGroupMapper;

    @ApiOperation(value = "分页查询报警规则列表", notes = "分页查询报警规则列表", response = AlarmRuleInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/alarmRule/findAlarmRuleList")
    public Object findAlarmRuleList(@RequestBody AlarmRuleQuery alarmRuleQuery){
        return alarmRuleService.findAlarmRuleList(alarmRuleQuery);
    }

    @SystemLogType(model = SystemModelEnum.AlarmRule, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "添加报警规则", notes = "添加报警规则")
    @ApiOperationSupport(order = 2)
    @PostMapping("/alarmRule/addAlarmRule")
    public Object addAlarmRule(@RequestBody AlarmRuleInfoDto alarmRuleInfoDto){
        alarmRuleService.addAlarmRule(alarmRuleInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.AlarmRule, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "编辑报警规则", notes = "编辑报警规则" )
    @ApiOperationSupport(order = 3)
    @PostMapping("/alarmRule/updateAlarmRule")
    public Object updateAlarmRule(@RequestBody AlarmRuleInfoDto alarmRuleInfoDto){
        alarmRuleService.updateAlarmRule(alarmRuleInfoDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.AlarmRule, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除", notes = "删除")
    @ApiOperationSupport(order = 4)
    @PostMapping("/alarmRule/deleteAlarmRule")
    public Object deleteAlarmRule(@RequestBody AlarmRuleIdDto alarmRuleIdDto){
        alarmRuleService.deleteAlarmRule(alarmRuleIdDto);
        return true;
    }

    @ApiOperation(value = "查询报警规则详情", notes = "查询报警规则详情", response = AlarmRuleInfoVo.class)
    @ApiOperationSupport(order = 5)
    @PostMapping("/alarmRule/findAlarmRuleInfo")
    public Object findAlarmRuleInfo(@RequestBody AlarmRuleIdDto alarmRuleIdDto){
        return alarmRuleService.findAlarmRuleInfo(alarmRuleIdDto);
    }

    @ApiOperation(value = "查询报警联系人列表", notes = "查询报警联系人列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 6)
    @PostMapping("/alarmRule/findAlarmContacterList")
    public Object findAlarmContacterList(){
        List<SelectKeyVo> selectIdVoList = new ArrayList<>();
        //报警联系人
        AlarmContacterQuery alarmContacterQuery = new AlarmContacterQuery();
        alarmContacterQuery.setMerId(getLoginInfo().getMerId());
        List<AlarmContacter> alarmContacterList = alarmContacterMapper.findByList(alarmContacterQuery);
        if(alarmContacterList != null){
            for (AlarmContacter alarmContacter : alarmContacterList){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(alarmContacter.getId()));
                selectKeyVo.setValue(alarmContacter.getName());
                selectIdVoList.add(selectKeyVo);
            }
        }

        //钉钉群报警
        DingWarningGroupQuery dingWarningGroupQuery = new DingWarningGroupQuery();
        dingWarningGroupQuery.setMerId(getLoginInfo().getMerId());
        List<DingWarningGroup> dingWarningGroupList = dingWarningGroupMapper.queryDingWarningGroupList(dingWarningGroupQuery);
        if(dingWarningGroupList != null){
            for (DingWarningGroup dingWarningGroup : dingWarningGroupList){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(dingWarningGroup.getId()));
                selectKeyVo.setValue(dingWarningGroup.getGroupName());
                selectIdVoList.add(selectKeyVo);
            }
        }
        return selectIdVoList;
    }

    @SystemLogType(model = SystemModelEnum.AlarmRule, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "设置报警规则开启关闭", notes = "设置报警规则开启关闭" )
    @ApiOperationSupport(order = 7)
    @PostMapping("/alarmRule/setAlarmRuleStatus")
    public Object setAlarmRuleStatus(@RequestBody AlarmRuleSetStatusDto alarmRuleSetStatusDto){
        alarmRuleService.setAlarmRuleStatus(alarmRuleSetStatusDto);
        return true;
    }

}
